{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "SkinDeep Code.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "cSpHhmCzdCpO"
      },
      "source": [
        "# **SkinDeep**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qZiQClqCdJGI"
      },
      "source": [
        "import fastai\n",
        "from fastai.vision import *\n",
        "from fastai.callbacks import *\n",
        "from fastai.vision.gan import *\n",
        "from torchvision.models import vgg16_bn\n",
        "from fastai.utils.mem import *"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "9WZR-ugCdN1u"
      },
      "source": [
        "# **Path**"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oRAK_KgLdXas"
      },
      "source": [
        "**Each Path has different set of Images.**"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Zqjv5rz4dNSm"
      },
      "source": [
        "path = Path('/content/gdrive/MyDrive/Tattoo')\n",
        "path_hr = Path('/content/gdrive/MyDrive/Tattoo/tattoo-r half')\n",
        "path_lr = Path('/content/gdrive/MyDrive/Tattoo/tattoo half')\n",
        "\n",
        "path_hr1 = Path('/content/gdrive/MyDrive/Tattoo/Split Tattoo-R')\n",
        "path_lr1 = Path('/content/gdrive/MyDrive/Tattoo/Split Tattoo')\n",
        "\n",
        "path_hr2 = Path('/content/gdrive/MyDrive/Tattoo/Tattoo-R')\n",
        "path_lr2 = Path('/content/gdrive/MyDrive/Tattoo/Tattoo')\n",
        "\n",
        "path_hr3 = Path('/content/gdrive/MyDrive/Tattoo/tat-r')\n",
        "path_lr3 = Path('/content/gdrive/MyDrive/Tattoo/tat')\n",
        "\n",
        "path_hr4 = Path('/content/gdrive/MyDrive/Tattoo/tatt-R')\n",
        "path_lr4 = Path('/content/gdrive/MyDrive/Tattoo/tatt')\n",
        "\n",
        "path_hr5 = Path('/content/gdrive/MyDrive/Tattoo/new-tattoo-r')\n",
        "path_lr5 = Path('/content/gdrive/MyDrive/Tattoo/new-tattoo')\n",
        "\n",
        "path_hr6 = Path('/content/gdrive/MyDrive/Tattoo/fresh tattoo-r')\n",
        "path_lr6 = Path('/content/gdrive/MyDrive/Tattoo/fresh tattoo')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QIEByjoaPh-M"
      },
      "source": [
        "bs,size=10,64\n",
        "arch = models.resnet34"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-8aJnWSSPkft"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr).split_by_rand_pct(0.3, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Al5iVI7FPlzU"
      },
      "source": [
        "additional_aug=(perspective_warp(magnitude = 0.25, p = 0.25))\n",
        "\n",
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5,xtra_tfms=additional_aug), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UgXm0p5gPnC9"
      },
      "source": [
        "data = get_data(bs,size)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JUYUuUyTdiZN"
      },
      "source": [
        "data.show_batch(ds_type=DatasetType.Valid, rows=2, figsize=(9,9))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-vjNUE2Rdmcd"
      },
      "source": [
        "t = data.valid_ds[0][1].data\n",
        "t = torch.stack([t,t])"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WGKcpddidoMu"
      },
      "source": [
        "def gram_matrix(x):\n",
        "    n,c,h,w = x.size()\n",
        "    x = x.view(n, c, -1)\n",
        "    return (x @ x.transpose(1,2))/(c*h*w)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dW35cY-4dpjV"
      },
      "source": [
        "gram_matrix(t)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "251T8WwYdrBV"
      },
      "source": [
        "base_loss = F.l1_loss\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E_LkRbgpdsRQ"
      },
      "source": [
        "vgg_m = vgg16_bn(True).features.cuda().eval()\n",
        "requires_grad(vgg_m, False)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fyeHUr4Vdtrl"
      },
      "source": [
        "blocks = [i-1 for i,o in enumerate(children(vgg_m)) if isinstance(o,nn.MaxPool2d)]\n",
        "blocks, [vgg_m[i] for i in blocks]"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "a2-2TcGLP0iE"
      },
      "source": [
        "class FeatureLoss(nn.Module):\n",
        "    def __init__(self, m_feat, layer_ids, layer_wgts):\n",
        "        super().__init__()\n",
        "        self.m_feat = m_feat\n",
        "        self.loss_features = [self.m_feat[i] for i in layer_ids]\n",
        "        self.hooks = hook_outputs(self.loss_features, detach=False)\n",
        "        self.wgts = layer_wgts\n",
        "        self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))\n",
        "              ] + [f'gram_{i}' for i in range(len(layer_ids))]\n",
        "\n",
        "    def make_features(self, x, clone=False):\n",
        "        self.m_feat(x)\n",
        "        return [(o.clone() if clone else o) for o in self.hooks.stored]\n",
        "    \n",
        "    def forward(self, input, target):\n",
        "        out_feat = self.make_features(target, clone=True)\n",
        "        in_feat = self.make_features(input)\n",
        "        self.feat_losses = [base_loss(input,target)]\n",
        "        self.feat_losses += [base_loss(f_in, f_out)*w\n",
        "                             for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]\n",
        "        self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out))*w**2 * 5e3\n",
        "                             for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]\n",
        "        self.metrics = dict(zip(self.metric_names, self.feat_losses))\n",
        "        return sum(self.feat_losses)\n",
        "    \n",
        "    def __del__(self): self.hooks.remove()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4sWsNuoodxlN"
      },
      "source": [
        "feat_loss = FeatureLoss(vgg_m, blocks[2:5], [5,15,2])\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MmeuQmVeP3-M"
      },
      "source": [
        "wd = 1e-3\n",
        "y_range = (-3.,3.)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "57B4mZWbP5N8"
      },
      "source": [
        "def create_gen_learner():\n",
        "    return unet_learner(data, arch, wd=wd, blur=True,norm_type=NormType.Spectral,self_attention=True, y_range=(-3.0, 3.0),\n",
        "                        loss_func=feat_loss, callback_fns=LossMetrics)\n",
        "gc.collect();"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VCk26GfjP6eG"
      },
      "source": [
        "learn_gen = create_gen_learner()\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HNt4LvUfd3F1"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5cVDnwdBd6Ks"
      },
      "source": [
        "lr = 8.32E-02\n",
        "epoch = 5\n",
        "def do_fit(save_name, lrs=slice(lr), pct_start=0.9):\n",
        "    learn_gen.fit_one_cycle(epoch, lrs, pct_start=pct_start)\n",
        "    learn_gen.save(save_name)\n",
        "    learn_gen.show_results(rows=1, imgsize=5)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PrzjFNFDd7oF"
      },
      "source": [
        "do_fit('da', slice(lr*10))\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9cBpYHgId-OU"
      },
      "source": [
        "learn_gen.show_results(rows=20)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bWWr3wsHeAls"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_8-TT7yIeB2U"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7nsuo6u2eDkk"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db', slice(1.32E-06,lr))\n",
        "#1e-05"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J-KtMPEYeFLs"
      },
      "source": [
        "learn_gen.show_results(rows=10)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JaR4imdFeHVM"
      },
      "source": [
        "data = get_data(8,128)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2YSrUXyNeIuk"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4OrCvBEreJzd"
      },
      "source": [
        "epoch =6\n",
        "lr = 3.98E-06\n",
        "do_fit('db2',slice(lr))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Bux6jBFNeNSd"
      },
      "source": [
        "learn_gen.show_results(rows=20)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x5WUwxD-ePJo"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EO_6sWlKeQT3"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "J4oNzVA6eSgl"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db3', slice(1.91E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KJyTiWxKeUcd"
      },
      "source": [
        "learn_gen.show_results(rows=10)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sTzJa2yyeXNW"
      },
      "source": [
        "data = get_data(10,192)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db3');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HWJUc0gYeYe1"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TOviVpsteamN"
      },
      "source": [
        "epoch =5\n",
        "lr = 6.92E-06\n",
        "do_fit('db4')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FDss5Mvfec3E"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "roV5Sa48eeU2"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cFcDGfKuegkN"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db5', slice(6.31E-07,1e-5), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ws6peh5Xp75h"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr2).split_by_rand_pct(0.2, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7rE_xtOwp75j"
      },
      "source": [
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr2/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vQaJy0ilp75k"
      },
      "source": [
        "data = get_data(20,64)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db5');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VfN3slDsemgV"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yEsZpvg_esav"
      },
      "source": [
        "epoch = 5\n",
        "lr = 3.63E-03\n",
        "do_fit('db6')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9Lhc8W7Geux8"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_ReAhVr9ev23"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ylqzlC1qexhE"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db7', slice(1.10E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qHbcNpCwezR0"
      },
      "source": [
        "data = get_data(10,128)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db7');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kpYZKS8ne1_F"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "V6E50tx_e4WE"
      },
      "source": [
        "epoch = 5\n",
        "lr = 1.10E-06\n",
        "do_fit('db8')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jD2Mq-_Oe7h0"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7rnL-EDle_68"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G27r7JTpfCjF"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db9', slice(1.10E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tQb9GeIHC9Nf"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr3).split_by_rand_pct(0.2, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GuYpEysmC9No"
      },
      "source": [
        "additional_aug=(perspective_warp(magnitude = 0.25, p = 0.25))\n",
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr3/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5,xtra_tfms=additional_aug), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fNEsaE7jfJBs"
      },
      "source": [
        "data = get_data(10,128)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db9');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XPsbQ2swfKk0"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "U7Nji8IbfR_8"
      },
      "source": [
        "epoch = 5\n",
        "lr = 2.29E-06\n",
        "do_fit('db10')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "feijswzrfT78"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mNUw7zTafVgz"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZHzFDhjOfW9T"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db11', slice(2.29E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2SJEkSaXfY2s"
      },
      "source": [
        "data = get_data(10,192)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db11');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1Ec-31TxfcVE"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cQUrftCDfd8F"
      },
      "source": [
        "epoch = 5\n",
        "lr = 2.29E-06\n",
        "do_fit('db12')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7aTSBasGDl-o"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nxV8xuMLDl-o"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JiiFa4z1fkS3"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db13', slice(9.12E-07,1e-5), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "t3swNL0ZQ4F7"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr4).split_by_rand_pct(0.2, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PflRfS9EQ4GI"
      },
      "source": [
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr4/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "n4h1hXBAQ4GJ"
      },
      "source": [
        "data = get_data(10,128)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db13');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HJEWGPFgfplU"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GpUHp58gfrh9"
      },
      "source": [
        "epoch = 5\n",
        "lr = 3.31E-06\n",
        "do_fit('db14')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KkVKXkRHRX4F"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4TtcRXBuRX4G"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HjBC_M_Mfyqw"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db15', slice(2.75E-06,1e-3), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PXHJYRLPfy-R"
      },
      "source": [
        "data = get_data(10,192)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db15');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ntOu07Hzf2xc"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JxShsRsCf3Fb"
      },
      "source": [
        "epoch = 5\n",
        "lr = 3.98E-06\n",
        "do_fit('db16')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dNSBEJznRmha"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Kwbcf1N0Rmhb"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zXkdXWHTf96s"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db17', slice(6.31E-07,1e-5), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o7QzA0tNDd1X"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr6).split_by_rand_pct(0.2, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "idko7dSfDd1c"
      },
      "source": [
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr6/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0mJy2_bFDd1c"
      },
      "source": [
        "data = get_data(10,192)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db17');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HgYOYYeKgEYU"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "E_fVGyYDgIDA"
      },
      "source": [
        "epoch = 5\n",
        "lr = 2.09E-05\n",
        "do_fit('db18')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LFPQB2nwDx7h"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qm5gR9gODx7i"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "T-iTJYxRgMZs"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db19', slice(1.91E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Vn3Y7jM3gQZc"
      },
      "source": [
        "data = get_data(9,256)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db19');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RFSBrn3vgRo1"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2qXN5jNogVOM"
      },
      "source": [
        "epoch = 5\n",
        "lr = 6.31E-07\n",
        "do_fit('db20')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "p1hdZ5IUwirE"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5Is8vXnmwirE"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "N7ZSf-IZtkPx"
      },
      "source": [
        "epoch = 5\n",
        "do_fit('db21', slice(1.58E-06,1e-4), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QsQifsvisEXT"
      },
      "source": [
        "src = ImageImageList.from_folder(path_lr5).split_by_rand_pct(0.2, seed=42)\n"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zTO8YL6isEXU"
      },
      "source": [
        "def get_data(bs,size):\n",
        "    data = (src.label_from_func(lambda x: path_hr5/x.name)\n",
        "           .transform(get_transforms(max_zoom=2., max_warp =0.25, max_lighting =0.5), size=size, tfm_y=True)\n",
        "           .databunch(bs=bs,num_workers = 0).normalize(imagenet_stats, do_y=True))\n",
        "\n",
        "    data.c = 3\n",
        "    return data"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LvtRqRKrtusd"
      },
      "source": [
        "data = get_data(5,320)\n",
        "learn_gen.data = data\n",
        "learn_gen.freeze()\n",
        "gc.collect()\n",
        "learn_gen.load('db21');"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6GiM72PBtzBz"
      },
      "source": [
        "data.show_batch(ds_type=DatasetType.Valid, rows=2, figsize=(9,9))"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JSoIh_h0t0oV"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UrfEaURnt2W6"
      },
      "source": [
        "epoch = 6\n",
        "lr = 6.31E-07\n",
        "do_fit('db22')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "eeiG9jbvsQ1S"
      },
      "source": [
        "learn_gen.unfreeze()"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nKlH4GJdsQ1S"
      },
      "source": [
        "learn_gen.lr_find()\n",
        "learn_gen.recorder.plot(suggestion =True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hF-6LgwUt7yK"
      },
      "source": [
        "epoch = 6\n",
        "do_fit('db23', slice(3.63E-05,1e-3), pct_start=0.3)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "at-INWdIuAWW"
      },
      "source": [
        "learn_gen.show_results(rows=30)"
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}